package maple.thelittleredbook.recommendation.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import maple.thelittleredbook.interaction.entity.UserBehavior;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 用户行为Mapper接口（推荐模块专用）
 * 专门负责推荐相关的用户行为查询和用户画像更新
 */
@Mapper
public interface RecommendationUserBehaviorMapper extends BaseMapper<UserBehavior> {

    /**
     * 统计冷却期内的同类行为数量
     *
     * @param userId 用户ID
     * @param contentId 内容ID
     * @param behaviorType 行为类型
     * @param cooldownBoundary 冷却边界时间
     * @return 冷却期内同类行为的数量
     */
    @Select("SELECT COUNT(*) FROM user_behaviors " +
            "WHERE user_id = #{userId} AND content_id = #{contentId} " +
            "AND behavior_type = #{behaviorType} AND created_at > #{cooldownBoundary}")
    int countRecentBehavior(@Param("userId") Long userId,
                            @Param("contentId") Long contentId,
                            @Param("behaviorType") String behaviorType,
                            @Param("cooldownBoundary") LocalDateTime cooldownBoundary);

    /**
     * 原子性更新用户画像
     * 使用向量加法进行画像旋转，允许超出[-1,1]范围
     *
     * @param userId 用户ID
     * @param dim1-dim8 各维度的增量值
     * @return 更新的记录数
     */
    @Update("UPDATE user_profiles SET " +
            "dim1 = LEAST(GREATEST(dim1 + #{dim1}, -999.99), 999.99), " +
            "dim2 = LEAST(GREATEST(dim2 + #{dim2}, -999.99), 999.99), " +
            "dim3 = LEAST(GREATEST(dim3 + #{dim3}, -999.99), 999.99), " +
            "dim4 = LEAST(GREATEST(dim4 + #{dim4}, -999.99), 999.99), " +
            "dim5 = LEAST(GREATEST(dim5 + #{dim5}, -999.99), 999.99), " +
            "dim6 = LEAST(GREATEST(dim6 + #{dim6}, -999.99), 999.99), " +
            "dim7 = LEAST(GREATEST(dim7 + #{dim7}, -999.99), 999.99), " +
            "dim8 = LEAST(GREATEST(dim8 + #{dim8}, -999.99), 999.99), " +
            "last_update_time = NOW() " +
            "WHERE user_id = #{userId}")
    int updateUserProfileByVector(@Param("userId") Long userId,
                                  @Param("dim1") BigDecimal dim1,
                                  @Param("dim2") BigDecimal dim2,
                                  @Param("dim3") BigDecimal dim3,
                                  @Param("dim4") BigDecimal dim4,
                                  @Param("dim5") BigDecimal dim5,
                                  @Param("dim6") BigDecimal dim6,
                                  @Param("dim7") BigDecimal dim7,
                                  @Param("dim8") BigDecimal dim8);

    /**
     * 统计用户对特定内容的特定行为次数
     *
     * @param userId 用户ID
     * @param contentId 内容ID
     * @param behaviorType 行为类型
     * @return 行为次数
     */
    @Select("SELECT COUNT(*) FROM user_behaviors " +
            "WHERE user_id = #{userId} AND content_id = #{contentId} AND behavior_type = #{behaviorType}")
    int countUserBehaviorByType(@Param("userId") Long userId,
                                @Param("contentId") Long contentId,
                                @Param("behaviorType") String behaviorType);
}